const Controller = require('../prototype/Controller')
const { models } = require('../model')

class Config extends Controller {
  constructor () {
    super({ model: models.Config })
  }

  findByConfigId () {
    return async (ctx) => {
      const query = this._getRequestQuery(ctx)
      const id = query.id
      const doc = await this.model.findOne({ id })
      return ctx.wrapper.succ(doc)
    }
  }

  findPublicConfigByConfigId () {
    const publicConfigs = [
      'WORKBENCH_DIRECTION'
    ]

    return async (ctx) => {
      const query = this._getRequestQuery(ctx)
      const id = query.id
      if (!publicConfigs.find(e => e === id)) {
        return ctx.wrapper.succ(null)
      }
      const doc = await this.model.findOne({ id })
      return ctx.wrapper.succ(doc)
    }
  }
}

module.exports = Config
